#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <winsock2.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>

#include "time_sun.h"
#include "file_sun.h"
#include "system_sun.h"

#pragma comment(lib,"ws2_32.lib")

/*******************************
//输入 要监听的本地ip、端口
//输入 console多少行clr，默认2048行
//输入 是否保存，y保存 n不保存
//输入 多少行保存为一个文件，默认4096行；
//输入 ctrl+c 保存当前文件后推出

//输入 要ping的设备ip、端口
//输入 console多少行clr，默认2048行
//输入 是否保存，y保存 n不保存
//输入 多少行保存为一个文件，默认4096行；
//输入 ctrl+c 保存当前文件后推出

*******************************/

#define NUM_LINE_ONCE_SHOW 200//命令行窗口一次显示行数

int netInit();


#define RECV_PACKET_MAX_LEN 2048

#pragma pack(1)
typedef struct {
    int size;
    struct sockaddr_in client;
    char packet[RECV_PACKET_MAX_LEN];
} sRequestDef;
#pragma pack()

sRequestDef sRequest;
WSADATA wsa;
SOCKET mySocket;
pthread_t t_id;
struct sockaddr_in local_addr;
int addr_len;
//char *localStrIp = "45.45.10.11";
char localStrIp[12];
int localPort = 5112;


int netInit()
{
    if(WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        printf("Init windows socket failed!\r\n");
        return -1;
    }

    mySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(mySocket == INVALID_SOCKET)
    {
        printf("Error at socket():\r\n");
        WSACleanup();
        return -1;
    }

    memset(&local_addr, 0, sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    local_addr.sin_port = htons(localPort);

    if(bind(mySocket, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
    {
        printf("bind failed\r\n");
        return -1;
    }
    else
    {
        printf("bind success\r\n");
    }
    return 0;
}

uint32_t oneWriteCount = 0;
uint16_t oneShowCount = 0;
//const char *CDIR = "E:\\myfile\\";
const char *CDIR = "./";

char firDir[30];
char timeStr[20];

void udpRcvProcess()
{
    SYSTEMTIME sysTime;
    sRequestDef *request = &sRequest;
    FILE *pFile = NULL;

    memset(request, 0, sizeof(sRequestDef));
    addr_len = sizeof(struct sockaddr_in);
    static WORD day = 0;

    while(1) {

        memset(request->packet, 0, sizeof(request->packet));
        request->size  = recvfrom (
                    mySocket, (char *)(request->packet), RECV_PACKET_MAX_LEN, 0,
                    (struct sockaddr *)&(request->client),
                    &addr_len);//阻塞等待消息到来

        getSysTime(&sysTime);//获取系统时间

        if(day != sysTime.wDay) {
            day = sysTime.wDay;//以天为单位，记录文件名称

            oneWriteCount = 0;

            if(pFile != NULL) {
                fileClose(pFile);
            }

            strcpy(firDir, CDIR);

            memset(timeStr, 0, sizeof(timeStr));
            sprintf(timeStr, "%4d%02d%02d",
                    sysTime.wYear,sysTime.wMonth,sysTime.wDay,
                    sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

            fileNew(CDIR, timeStr, &pFile);
        }

        memset(timeStr, 0, sizeof(timeStr));
        sprintf(timeStr, "%5ld,%4d%02d%02d,%02d%02d%02d,%03d:",
                oneWriteCount,
                sysTime.wYear,sysTime.wMonth,sysTime.wDay,
                sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

        fileWrite(pFile, timeStr, strlen(timeStr));

        fileWrite(pFile, request->packet, request->size);
        printf("%s%s ", timeStr, request->packet);
        oneWriteCount++;
        oneShowCount++;

        if(oneShowCount == NUM_LINE_ONCE_SHOW) {
            oneShowCount = 0;
            system("cls");
        }
    }
}

int main(int argc, char *argv[])
{

    netInit();

    udpRcvProcess();

    return 0;
}
